<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="UTF-8">
    <title>角色管理页面</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta http-equiv="Access-Control-Allow-Origin" content="*">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="icon" href="../resources/favicon.ico">
    <link rel="stylesheet" href="../resources/layui/css/layui.css" media="all"/>
    <link rel="stylesheet" href="../resources/css/public.css" media="all"/>
    <link rel="stylesheet" href="../resources/layui_ext/dtree/dtree.css" media="all"/>
    <link rel="stylesheet" href="../resources/layui_ext/dtree/font/dtreefont.css" media="all"/>
</head>
<!--查询条件-->
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 15px;">
    <legend>搜索条件</legend>
</fieldset>
<form action="" method="post" id="searchFrm" lay-filter="searchFrm" class="layui-form">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">角色名称</label>
            <div class="layui-input-inline">
                <input type="text" name="name" id="name" class="layui-input input-radius"
                       placeholder="请输入角色名称">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">角色备注</label>
            <div class="layui-input-inline">
                <input type="text" name="remark" id="remark" class="layui-input input-radius" placeholder="请输入角色备注">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">是否可用</label>
            <div class="layui-input-inline">
                <input type="radio" name="available" value="1" title="可用">
                <input type="radio" name="available" value="0" title="不可用">
            </div>
        </div>
        <div class="layui-inline">
            <button type="button" class="layui-btn layui-btn-sm layui-btn-radius" lay-submit="" lay-filter="doSearch"><i
                    class="layui-icon layui-icon-search layui-icon-normal"></i>查询
            </button>
            <button type="reset" class="layui-btn layui-btn-sm layui-btn-radius layui-btn-warm"><i
                    class="layui-icon layui-icon-refresh"></i><span>重置</span>
            </button>
        </div>
    </div>
</form>

<!--数据表格-->
<div>
    <table class="layui-hide" id="roleTable" lay-filter="roleTable"></table>
    <div id="roleToolBar" style="display: none">
        <button type="button" lay-event="add" shiro:hasPermission="sys:role:add"
                class="layui-btn layui-btn-sm layui-btn-normal layui-btn-radius">
            <i class="layui-icon layui-icon-add-1"></i>添加角色
        </button>
    </div>
    <div id="roleRowBar" style="display: none;">
        <button type="button" lay-event="update" shiro:hasPermission="sys:role:edit"
                class="layui-btn layui-btn-xs layui-btn-radius"><i
                class="layui-icon layui-icon-edit"></i>编辑
        </button>
        <button type="button" lay-event="detail" shiro:hasPermission="sys:role:edit"
                class="layui-btn layui-btn-xs layui-btn-radius"><i
                class="layui-icon layui-icon-username"></i>分配用户
        </button>
        <button type="button" lay-event="delete" shiro:hasPermission="sys:role:delete"
                class="layui-btn layui-btn-xs layui-btn-danger layui-btn-radius"><i
                class="layui-icon layui-icon-delete"></i>删除
        </button>
    </div>
</div>

<script type="text/javascript" src="../resources/layui/layui.js"></script>

<script type="text/javascript">
    var ctx = [[@{/}]];
    layui.extend({
        dtree: ctx + '/resources/layui_ext/dtree/dtree'
    }).use(['dtree', 'jquery', 'form', 'layer', 'laydate', 'table'], function () {
        var $ = layui.jquery;
        var form = layui.form;
        var layer = layui.layer;
        var laydate = layui.laydate;
        var table = layui.table;
        var dtree = layui.dtree;
        //初始化时间选择器
        laydate.render({
            elem: "#startTime",
            type: "datetime"
        });
        laydate.render({
            elem: "#endTime",
            type: "datetime"
        });

        //初始化表格
        var tableIns = table.render({
            elem: "#roleTable",
            title: "角色数据表格",
            url: ctx + "/role/loadAllRole",
            method: 'post',
            toolbar: "#roleToolBar",
            page: true,
            height: "full-120",
            limit: 8,
            request: {
                pageName: "pageNum",
                limitName: "pageSize"
            },
            response: {
                statusName: 'code', //状态字段名称
                statusCode: '0', //状态字段成功值
                msgName: 'msg', //消息字段
                countName: 'total',
                dataName: "rows"
            },
            cols: [ [
                {field: 'name', title: '角色名称', align: 'center'},
                {field: 'remark', title: '角色备注', align: 'center'},
                {
                    field: 'available', title: '是否可用', align: 'center', templet: function (d) {
                        return d.available == 1 ? '<font color="blue">可用</font>' : '<font color="red">不可用</font>';
                    }
                },
                {field: 'createTime', title: '创建时间', align: 'center'},
                {fixed: 'right', title: '操作', toolbar: '#roleRowBar', align: 'center', width: 280}
            ] ]
        });

        //点击重置按钮时，重置表单的值
        $("#resetRole").click(function () {
            $("#searchFrm")[0].reset();
        });

        //监控模糊查询按钮事件
        form.on("submit(doSearch)", function (data) {
            var params = $("#searchFrm").serialize();
            tableIns.reload({
                where: data.field,
                url: ctx + "/role/loadAllRole",
                method: "post",
                page: {
                    curr: 1
                }
            });
            return false;
        });

        //监控工具条事件
        table.on("toolbar(roleTable)", function (obj) {
            switch (obj.event) {
                case 'add':
                    openAddLayer();
                    break;
            }
            ;
        });

        //监控行工具条事件
        table.on("tool(roleTable)", function (obj) {
            //获取当前行数据
            var data = obj.data;
            switch (obj.event) {
                case 'update':
                    updateRole(data);
                    break;
                case 'delete':
                    deleteRole(data);
                    break;
                case 'detail':
                    detailRole(data);
                    break;
            }
            ;
        });

        var mainIndex;
        var url;

        //打开添加弹出层
        function openAddLayer() {
            mainIndex = layer.open({
                type: 2,
                content: ctx + '/role/addRoleIndex',
                area: ['800px', '550px'],
                title: '添加角色',
                btn: ["确定", '关闭'],
                yes: function (index, layerOpen) {
                    var iframeWin = layerOpen.find('iframe')[0];
                    iframeWin.contentWindow.submitHandler(index, layerOpen);
                }
            });
        }

        //打开修改弹出层
        function updateRole(data) {
            var id = data.id;
            mainIndex = layer.open({
                type: 2,
                content: ctx + '/role/editRoleIndex?id=' + id,
                area: ['800px', '550px'],
                title: '修改角色',
                btn: ["确定", '关闭'],
                yes: function (index, layerOpen) {
                    var iframeWin = layerOpen.find('iframe')[0];
                    iframeWin.contentWindow.submitHandler(index, layerOpen);
                }
            });
        }

        //提交按钮事件
        form.on("submit(doSubmit)", function (data) {
            $.post(url, data.field, function (res) {
                if (res.code == 0) {
                    tableIns.reload();
                }
                layer.msg(res.msg);
                layer.close(mainIndex);
            });
            return false;
        });

        //删除
        function deleteRole(data) {
            layer.confirm('你确定要删除【' + data.name + '】这条数据吗？如果您删除该角色，那么所有拥有该角色的用户将没有角色，请重新给这些用户分配角色！', {
                icon: 3,
                title: '提示'
            }, function (index) {
                $.post(ctx + "/role/deleteRole", {id: data.id}, function (res) {
                    if (res.code == 0) {
                        tableIns.reload({
                            where: "",
                        });
                    }
                    layer.msg(res.msg);
                });
                layer.close(index);
            });
        }

        // 给角色分配用户
        function detailRole(data) {
            mainIndex = layer.open({
                type: 2,
                content: ctx + '/role/getSetRoleIndex/' + data.id,
                //
                area: ['1334px', '95%'],
                title: '分配用户',
                shade: 0,
                btn: ['关闭'],
                yes: function (index, layerOpen) {
                    layer.close(index);
                }
            });
        }
    });
</script>

</body>
</html>